package org.liveboardgames.agricola.control.delegates.action.processor
{
	import mx.collections.ArrayCollection;
	import mx.collections.ListCollectionView;

	import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
	import org.liveboardgames.agricola.control.delegates.mock.ActionFactory;
	import org.liveboardgames.agricola.control.events.intern.ActionEvent;
	import org.liveboardgames.agricola.model.AgricolaModelLocator;
	import org.liveboardgames.agricola.model.enums.RoundOffsetTypeEnum;
	import org.liveboardgames.agricola.vo.Game;
	import org.liveboardgames.agricola.vo.action.Action;
	import org.liveboardgames.agricola.vo.action.ResourcesOnBoardAction;
	import org.liveboardgames.agricola.vo.board.BoardTile;
	import org.liveboardgames.agricola.vo.player.Player;
	import org.liveboardgames.common.dictionary.collections.IMap;

	public class AddResourceOnBoardActionProcessor extends ActionProcessor
	{
		public function AddResourceOnBoardActionProcessor(action:Action)
		{
			super(action);
		}

		override public function execute():void
		{
			var player:Player = action.target as Player;

			// Get the parameters of the action

			if (action is ResourcesOnBoardAction)
			{
				var resourceAction:ResourcesOnBoardAction = ResourcesOnBoardAction(action);

				var resourceType:String = resourceAction.resourceType;
				var roundNumbers:ListCollectionView = resourceAction.rounds;
				var quantity:int = resourceAction.quantity;
				var roundOffsetType:String = resourceAction.offsetType;

				// Now we need to put the proper resources at the proper place on the board
				var game:Game = AgricolaModelLocator.getInstance().game;

				// Compute the number of the rounds where to put the resource
				var realRoundNumbers:ListCollectionView = computeRealRoundNumbers(game, roundNumbers, roundOffsetType);

				// Add the resource on each tile
				for each (var round:int in realRoundNumbers)
				{
					var boardTile:BoardTile = GameHelper.getTileForRound(game, round);
					boardTile.addResource(resourceType, quantity);
				}

			}

			this.dispatchEvent(new ActionEvent(ActionEvent.ACTION_COMPLETE));
		}

		private function computeRealRoundNumbers(game:Game, roundNumbers:ListCollectionView, offsetType:String):ListCollectionView
		{
			var realRoundNumbers:ListCollectionView = new ArrayCollection();
			// If the round number are absolute, nothing to do
			if (offsetType == RoundOffsetTypeEnum.ROUND_NUMBER)
			{
				realRoundNumbers = roundNumbers;
			}
			// Else we need to get the absolute value of these rounds
			else if (offsetType == RoundOffsetTypeEnum.ADD)
			{
				var currentRound:int = GameHelper.getCurrentRoundNumber(game);
				for each (var roundNumber:int in roundNumbers)
				{
					var newRoundNumber:int = roundNumber + currentRound;
					if (newRoundNumber <= GameHelper.NUMBER_OF_TURNS)
					{
						realRoundNumbers.addItem(newRoundNumber);
					}
				}
			}
			return realRoundNumbers;
		}
	}
}